﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace Sapiens.at.SharePoint.Controls
{
    /// <summary>
    /// Shows the SharePoint notification after the page got loaded.
    /// </summary>
    public class Notification : UserControl
    {
        /// <summary>
        /// Gets or sets the text of the notification.
        /// </summary>
        public string Text { get; set; }
        
        /// <summary>
        /// Adds the necessarry javascript to the page.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            RenderNotification(Page, Text);
        }

        /// <summary>
        /// Adds the necessarry javascript to a page.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="Text"></param>
        public static void RenderNotification(Page page, string Text)
        {
            // Add SP.js
            ScriptLink.RegisterScriptAfterUI(page, "SP.js", false, true);
            ScriptLink.RegisterScriptAfterUI(page, "SP.Ribbon.js", false, true);
            
            var script = string.Format(@"
                ExecuteOrDelayUntilScriptLoaded(showNotification, 'SP.js');
                function showNotification(){{
                    SP.UI.Notify.addNotification('{0}', false); // message, sticky
                }}
            ", Text);

            ScriptManager.RegisterClientScriptBlock(page, typeof(Notification), string.Format("Notification:{0}", Text), script, true);
        }
    }
}
